/**
 *  Copyright (c) 2005 by David Bridgewater
 *  All rights reserved.
 *  
 *  You may study, use, modify, and distribute this
 *  software for any purpose provided that this
 *  copyright notice appears in all copies.
 *  
 *  This software is provided without warranty
 *  either expressed or implied.
 */
package webcert.ch02.ex0201;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * ShowContext - a servlet to display the context name, and show
 * all the resource in the web application.
 * It even shows off its own source. 
 * 
 */
public class ShowContext extends HttpServlet {

    public static final String LT_SYMBOL = "<";

    public static final String GT_SYMBOL = ">";

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        /* Set the response type, retrieve the writer */
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out
                .write("<HTML><HEAD><TITLE>Show Context Servlet</TITLE></HEAD><BODY>");
        out.write("<H1>Show Context Servlet</H1>");

        /* The context path name is in the request */
        String ctxPath = request.getContextPath();
        out.write("<H2>The name of the context is " + ctxPath + "</H2>");

        /* This is the technique for getting the root path of the context */
        ServletContext sc = getServletContext();
        String ctxRealPath = sc.getRealPath("/");
        out.write("<H2>The name of the root path for the context is "
                + ctxRealPath + "</H2>");

        /* List the file contents at the root of the context */
        out.write("<H3>A listing of files & directories from the context root follows...</H3>");
        File f = new File(ctxRealPath);
        listFiles(f, out);

        /* List this source file */
        out.write("<H3>A source listing of this servlet follows...</H3>");
        out.write("<CODE>");
        f = new File(sc
                .getRealPath("/WEB-INF/src/webcert/ch02/ex0201/ShowContext.java"));
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line;
        while ((line = br.readLine()) != null) {
            line = clean(line);
            out.write("<BR />" + line);
        }
        out.write("</CODE>");

        /* Tidy up */
        out.write("</BODY></HTML>");
        out.flush();
        out.close();

    }

    /*
     * Lists all files from given location downwards; designed to be called
     * recursively.
     */
    private void listFiles(File f, PrintWriter out) {
        if (f.isFile()) {
            out.write("<BR />File: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + f.getPath());
            return;
        }
        if (f.isDirectory()) {
            out.write("<BR />Directory: " + f.getPath());
            File[] files = f.listFiles();
            for (int i = 0; i < files.length; i++) {
                listFiles(files[i], out);
            }
        }
    }

    /*
     * Cleans out the " <" and ">" symbols in the code and substitutes a
     * browser-friendly equivalent. Otherwise, the symbols in the code listing
     * are treated as HTML tags!
     */
    private String clean(String line) {
        int index;
        StringBuffer sb = new StringBuffer(line);
        while ((index = sb.indexOf(LT_SYMBOL)) > -1) {
            sb.replace(index, index + 1, "&");
            sb.insert(index + 1, "lt;");
        }
        while ((index = sb.indexOf(GT_SYMBOL)) > -1) {
            sb.replace(index, index + 1, "&");
            sb.insert(index + 1, "gt;");
        }
        return sb.toString();

    }
}